<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="活动" prop="activityId">
        <el-select
          v-model="queryParams.activityId"
          placeholder="请选择活动"
          clearable
          size="small"
          @keyup.enter.native="handleQuery">
            <el-option v-for="(item,index) in activityList" :label="item.name" :value="item.id" :key="index">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="corpName">
        <el-input
          v-model="queryParams.corpName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option  v-for="dict in statusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="活动类型" prop="type">-->
<!--        <el-select v-model="queryParams.type" placeholder="请选择活动类型" clearable size="small">-->
<!--          <el-option label="个人" value="0"  />-->
<!--          <el-option label="企业" value="1"  />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ypp:enroll:exportSiteEnroll']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-success"
          size="mini"
          @click="handleCheck"
          v-hasPermi="['ypp:enroll:checkapplybatch']"
        >报名通过</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-success"
          size="mini"
          @click="handleCheck2"
          v-hasPermi="['ypp:enroll:checkoutbatch']"
        >取消报名通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          :disabled="multiple"
          icon="el-icon-success"
          size="mini"
          @click="handleAuditBack"
          v-hasPermi="['ypp:enroll:rollBackAudit']"
        >取消报名待审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          :disabled="multiple"
          icon="el-icon-success"
          size="mini"
          @click="handleCheckOut"
          v-hasPermi="['ypp:enroll:checkapply']"
        >报名不通过</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="enrollList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="活动" align="center" prop="activityName" >
        <template slot-scope="scope">
          <el-button type="text" @click="openActivity(scope.row.activityId)">{{scope.row.activityName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="realName" >
        <template slot-scope="scope">
          <el-button type="text" @click="openUser(scope.row.wxUserId)">{{scope.row.name}}</el-button>
        </template>
      </el-table-column>
      <el-table-column  label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <span v-if="scope.row.gender == 0">未知</span>
          <span v-if="scope.row.gender == 1">男</span>
          <span v-if="scope.row.gender == 2">女</span>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" align="center" prop="corpName" />
      <el-table-column label="报名时间" align="center" prop ="createTime"/>
<!--      <el-table-column label="支付方式" align="center" prop="payWay" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="实付金额" align="center" prop="enrollCost" />
      <el-table-column label="付款单号" align="center" prop="payNo"/>
      <el-table-column label="付款时间" align="center" prop="payTime"/>
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="取消原因" align="center" prop="outCause" />-->
<!--      <el-table-column label="活动类型" align="center" prop="type" >-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.type == 0">个人</span>-->
<!--          <span v-if="scope.row.type == 1">企业</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleCheckOut(scope.row)"
            v-hasPermi="['hps:enroll:checkapply']"
            v-if="scope.row.status==0"
          >审核不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleCheckOut2(scope.row)"
            v-hasPermi="['hps:enroll:checkout']"
            v-if="scope.row.status==4"
          >取消不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            style="color: #ff3f3f"
            @click="handleCheckQuery(scope.row)"
            v-if="scope.row.status==3 || scope.row.status==5"
          >未通过原因</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 报名人对话框 -->
    <el-dialog  width="500px"  title="会员信息" :visible.sync="dialogFormVisible" @close="close">
      <el-descriptions border column="2">
        <el-descriptions-item  label="姓名">
          {{user.realName}}
        </el-descriptions-item>
        <el-descriptions-item  label="年龄">
          {{user.age}}
        </el-descriptions-item>
        <el-descriptions-item label="性别">
          <span v-if="user.gender==1">男</span><span v-if="user.gender==2">女</span>
        </el-descriptions-item>
        <el-descriptions-item  label="电话">
          {{user.phone}}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!-- 报名活动框 -->
    <el-dialog  width="500px" title="活动信息" :visible.sync="dialogActivityFormVisible" @close="close">
      <el-descriptions border column="2">
        <el-descriptions-item  label="活动名称">
          {{activity.name}}
        </el-descriptions-item>
        <el-descriptions-item  label="活动时间">
          {{activity.startTime}} - {{activity.activityEndTime}}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 审核不通过，审核意见 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >

        <el-form-item label="报名审核意见" prop="outCause" v-if="check==1">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="报名审核意见"
            v-model="form.outCause">
          </el-input>
        </el-form-item>

        <el-form-item label="退赛审核意见" prop="outCause" v-if="check==2">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="退赛审核意见"
            v-model="form.outCause">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="readLoad">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="visible1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="原因" prop="applyAudit">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="审核意见"
            v-model="form.outCause"
            readonly>
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visible1 = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import{ getUser } from '@/api/wx/user';
import { listEnroll, getEnroll, delEnroll, addEnroll, updateEnroll, exportEnroll,checkApply, checkApplyBatch, checkOut, checkOutBatch, rollBackAudit } from "@/api/ypp/enroll";
import {getActivity, listActivity} from "@/api/ypp/activity";

export default {
  name: "Enroll",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 活动报名表格数据
      enrollList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      visible1: false,
      //判断报名或退赛、1报名、2退赛
      check:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: undefined,
        wxUserId: undefined,
        name: undefined,
        corpName: undefined,
        payWay: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      activityList:[],
      statusOptions:[],
      user:{},
      dialogFormVisible:false,
      readLoad:false,
      dialogActivityFormVisible:false,
      activity:{}
    };
  },
  created() {
    this.getActivityList();
    this.getList();
  },
  methods: {
    openActivity(id){
      getActivity(id).then(response => {
        this.activity = response.data;
        this.dialogActivityFormVisible=true;
      });
    },
    openUser(id){
      getUser(id).then(response => {
        this.user = response.data;
        this.dialogFormVisible=true;
      });
    },
    // 状态类型字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询活动报名列表 */
    getList() {
      this.loading = true;
      listEnroll(this.queryParams).then(response => {
        this.enrollList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getActivityList(){
      this.getDicts("ypp_activity_enroll_status").then(response => {
        this.statusOptions = response.data;
      });
      listActivity().then(response => {
        this.activityList = response.rows;
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.check = 0;
    },
    // 表单重置
    reset() {
      this.check = 0;
      this.form = {
        id: undefined,
        activityId: undefined,
        wxUserId: undefined,
        name: undefined,
        corpName: undefined,
        payWay: undefined,
        status: "0",
        delFlag: undefined,
        enrollCost: undefined,
        remark: undefined,
        outCause: undefined,
        createTime: undefined,
        updateTime: undefined,
        type: undefined,
        membership: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动报名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEnroll(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动报名";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            if(this.check==1){

              let query = ""
              if (this.ids.length > 0) {
                for (let i = 0; i < this.ids.length;i++) {
                  this.ids[i] = "ids=" + this.ids[i]
                }
                query = this.ids.join("&")
              } else {
                query = "ids=" + this.form.id;
              }
              if (this.form.outCause != undefined && this.form.outCause != "") {
                query = query + "&outCause=" + this.form.outCause;
              }
              this.syncData(query);
            }

            if(this.check==2){

              checkOut(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("操作成功");
                  this.open = false;
                  this.getList();
                }
              });
            }

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除活动报名编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEnroll(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动报名数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEnroll(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 报名审核不通过 */
    handleCheckOut(row) {
      this.reset();
      let id = row.id || this.ids

      this.check=1;
      this.form.id = id;
      this.open = true;
      this.title = "审核意见";
    },
    // 报名成功退回待审核状态
    handleAuditBack(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认退回待审核状态，报名信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return rollBackAudit(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("退回成功");
      }).catch(function() {});
    },

    /** 退赛审核不通过 */
    handleCheckOut2(row) {
      this.reset();
      const id = row.id || this.ids
      this.check=2;
      this.form.id = id;
      this.open = true;
      this.title = "审核意见";
      // this.check=0;
    },
    // 查看审核不通过原因
    handleCheckQuery(row) {
      this.reset();
      const id = row.id || this.ids
      getEnroll(id).then(response => {
        this.title = "未通过原因";
        this.form.outCause = row.outCause
        this.visible1 = true
      });
    },
    syncData(query){
      var that = this;
      this.readLoad = true;
      checkApply(query).then(response => {
        that.getList();
        that.readLoad = false;
        that.$alert(response.msg, "生成结果", { dangerouslyUseHTMLString: true,type: "success" });
        that.open = false;
      }).catch(function(res) {
        that.getList();
        that.readLoad = false;
        that.open = false;
      });
    },

    /** 报名审核按钮操作 */
    handleCheck(row) {
      const ids = row.id || this.ids;
      console.log(ids)
      if(ids==undefined || ids==null ||ids.length==0){
        this.$message.error('请勾选至少一个待审核报名');
      }else{
        this.$confirm('是否确认审核通过报名信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return checkApplyBatch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function() {});
      }

    },
    /** 取消审核按钮操作 */
    handleCheck2(row) {
      const ids = row.id || this.ids;
      if(ids==undefined || ids==null ||ids.length==0){
        this.$message.error('请勾选至少一个待取消报名');
      }else{
        this.$confirm('是否确认审核通过报名信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return checkOutBatch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function() {});
      }

    },
    close(){
        this.dialogFormVisible=false;
    }
  }

};
</script>
